import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 完整流程测试
 * 模拟从原始数据到最终显示的完整转换流程
 */
public class CompleteFlowTest {
    
    // 模拟 RecoveryChartVO
    static class MockRecoveryChartVO {
        private String name;
        private String value;
        private String unit;
        
        public MockRecoveryChartVO(String name, String value, String unit) {
            this.name = name;
            this.value = value;
            this.unit = unit;
        }
        
        // getters
        public String getName() { return name; }
        public String getValue() { return value; }
        public String getUnit() { return unit; }
    }
    
    public static void main(String[] args) {
        System.out.println("=== 完整流程测试 ===");
        
        // 模拟原始数据处理流程
        testCompleteFlow();
        
        // 验证边界情况
        testBoundaryFlow();
    }
    
    /**
     * 测试完整流程
     */
    private static void testCompleteFlow() {
        System.out.println("\n--- 完整数据流程测试 ---");
        
        // 步骤1：模拟原始数据（kg, g）
        System.out.println("步骤1：原始数据");
        List<RawData> rawDataList = new ArrayList<>();
        rawDataList.add(new RawData("纸品类", new BigDecimal("672669"), "kg"));
        rawDataList.add(new RawData("旧衣类", new BigDecimal("686"), "kg"));
        rawDataList.add(new RawData("塑料类", new BigDecimal("37"), "kg"));
        rawDataList.add(new RawData("贵重金属", new BigDecimal("119"), "g"));
        rawDataList.add(new RawData("车辆类", new BigDecimal("5"), "辆"));
        rawDataList.add(new RawData("家电", new BigDecimal("8"), "台"));
        
        for (RawData data : rawDataList) {
            System.out.println("  " + data.name + ": " + data.value + data.unit);
        }
        
        // 步骤2：转换为标准单位并格式化显示
        System.out.println("\n步骤2：转换为chartTypeList格式");
        List<MockRecoveryChartVO> chartList = new ArrayList<>();
        
        for (RawData data : rawDataList) {
            BigDecimal convertedValue = convertToStandardUnit(data.value, data.unit);
            String formattedValue = formatValueWithUnit(convertedValue, data.unit);
            String finalUnit = getFinalUnit(convertedValue, data.unit);
            
            chartList.add(new MockRecoveryChartVO(data.name, formattedValue, finalUnit));
            
            System.out.println("  " + data.name + ": " + data.value + data.unit + 
                             " -> " + convertedValue.setScale(6, RoundingMode.HALF_UP) + "吨" +
                             " -> \"" + formattedValue + "\" (unit: " + finalUnit + ")");
        }
        
        // 步骤3：统计总量
        System.out.println("\n步骤3：统计总量");
        BigDecimal weightTotal = BigDecimal.ZERO;
        BigDecimal countTotal2 = BigDecimal.ZERO; // 台数
        BigDecimal countTotal3 = BigDecimal.ZERO; // 辆数
        
        for (MockRecoveryChartVO chartVO : chartList) {
            BigDecimal numericValue = extractNumericValue(chartVO.getValue());
            String unit = chartVO.getUnit().toLowerCase().trim();
            
            switch (unit) {
                case "台":
                    countTotal2 = countTotal2.add(numericValue);
                    System.out.println("  台数累加: " + numericValue + " (总计: " + countTotal2 + ")");
                    break;
                case "辆":
                    countTotal3 = countTotal3.add(numericValue);
                    System.out.println("  辆数累加: " + numericValue + " (总计: " + countTotal3 + ")");
                    break;
                case "吨":
                    weightTotal = weightTotal.add(numericValue);
                    System.out.println("  重量累加: " + numericValue + "吨 (总计: " + weightTotal + "吨)");
                    break;
                case "千吨":
                    BigDecimal tonValue = numericValue.multiply(new BigDecimal("1000"));
                    weightTotal = weightTotal.add(tonValue);
                    System.out.println("  重量累加: " + numericValue + "千吨 = " + tonValue + "吨 (总计: " + weightTotal + "吨)");
                    break;
            }
        }
        
        // 步骤4：最终显示
        System.out.println("\n步骤4：最终统计结果");
        System.out.println("  重量总计: " + formatWeightDisplay(weightTotal));
        System.out.println("  台数总计: " + countTotal2.setScale(0, RoundingMode.HALF_UP) + "台");
        System.out.println("  辆数总计: " + countTotal3.setScale(0, RoundingMode.HALF_UP) + "辆");
        
        System.out.println("\n最终chartTypeList:");
        for (MockRecoveryChartVO chartVO : chartList) {
            System.out.println("  {\"name\": \"" + chartVO.getName() + "\", \"value\": \"" + chartVO.getValue() + "\", \"unit\": \"" + chartVO.getUnit() + "\"}");
        }
    }
    
    /**
     * 测试边界情况流程
     */
    private static void testBoundaryFlow() {
        System.out.println("\n--- 边界情况流程测试 ---");
        
        // 测试接近1000吨的情况
        List<RawData> boundaryData = new ArrayList<>();
        boundaryData.add(new RawData("接近1000吨", new BigDecimal("999990"), "kg"));  // 999.99吨
        boundaryData.add(new RawData("正好1000吨", new BigDecimal("1000000"), "kg")); // 1000吨
        boundaryData.add(new RawData("超过1000吨", new BigDecimal("1200000"), "kg")); // 1200吨
        
        for (RawData data : boundaryData) {
            BigDecimal convertedValue = convertToStandardUnit(data.value, data.unit);
            String formattedValue = formatValueWithUnit(convertedValue, data.unit);
            String finalUnit = getFinalUnit(convertedValue, data.unit);
            
            System.out.println(data.name + ": " + data.value + data.unit + 
                             " -> " + convertedValue.setScale(2, RoundingMode.HALF_UP) + "吨" +
                             " -> \"" + formattedValue + "\" (unit: " + finalUnit + ")");
        }
    }
    
    // 辅助类和方法
    static class RawData {
        String name;
        BigDecimal value;
        String unit;
        
        RawData(String name, BigDecimal value, String unit) {
            this.name = name;
            this.value = value;
            this.unit = unit;
        }
    }
    
    private static BigDecimal convertToStandardUnit(BigDecimal value, String unit) {
        String unitLower = unit.toLowerCase().trim();
        switch (unitLower) {
            case "kg":
            case "千克":
                return value.divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP);
            case "g":
            case "克":
                return value.divide(new BigDecimal("1000000"), 6, RoundingMode.HALF_UP);
            case "台":
            case "辆":
            case "个":
            case "件":
            case "吨":
            case "t":
            default:
                return value;
        }
    }
    
    private static String formatValueWithUnit(BigDecimal value, String originalUnit) {
        String unitLower = originalUnit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
            case "kg":
            case "千克":
            case "g":
            case "克":
                // 重量类型：根据大小决定显示吨还是千吨
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
                    BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
                    return weightInKiloTons.toString() + "千吨";
                } else {
                    return formattedWeight.toString() + "吨";
                }
            case "台":
            case "辆":
            case "个":
            case "件":
                // 计数类型：保留整数并添加单位
                BigDecimal countValue = value.setScale(0, RoundingMode.HALF_UP);
                return countValue.toString() + originalUnit;
            default:
                return value.setScale(2, RoundingMode.HALF_UP).toString();
        }
    }
    
    private static String getFinalUnit(BigDecimal value, String originalUnit) {
        String unitLower = originalUnit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
            case "kg":
            case "千克":
            case "g":
            case "克":
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                return formattedWeight.compareTo(new BigDecimal("1000")) >= 0 ? "千吨" : "吨";
            default:
                return originalUnit;
        }
    }
    
    private static String formatWeightDisplay(BigDecimal weightInTons) {
        if (weightInTons == null) {
            return "0.00吨";
        }
        
        BigDecimal formattedWeight = weightInTons.setScale(2, RoundingMode.HALF_UP);
        
        if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
            BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
            return weightInKiloTons.toString() + "千吨";
        } else {
            return formattedWeight.toString() + "吨";
        }
    }
    
    private static BigDecimal extractNumericValue(String valueWithUnit) {
        if (valueWithUnit == null || valueWithUnit.trim().isEmpty()) {
            return BigDecimal.ZERO;
        }
        
        String numericPart = valueWithUnit.replaceAll("[^0-9.]", "");
        
        try {
            return new BigDecimal(numericPart);
        } catch (NumberFormatException e) {
            return BigDecimal.ZERO;
        }
    }
}
